<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Character.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Guild.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/base_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/character_DA.php');

class guild_DA extends base_DA {

    /* database field name */
    const FL_ID = 'id';
    const FL_NAME = 'name';
    const FL_API_KEY = 'api_key';
    const FL_RACE = 'race';
    const FL_ICON = 'icon';
    const FL_CREATION_DATE = 'creation_date';
    const FL_SHARD = 'shard';
    const FL_DESCRIPTION = 'description';
    const FL_MONEY = 'money';
    const FL_CIVILIZATION = 'civilization';
    const FL_CULT = 'cult';
    const FL_FYROS_FAME = 'fyros_fame';
    const FL_KAMI_FAME = 'kami_fame';
    const FL_KARAVAN_FAME = 'karavan_fame';
    const FL_MATIS_FAME = 'matis_fame';
    const FL_TRYKER_FAME = 'tryker_fame';
    const FL_ZORAI_FAME = 'zorai_fame';
    const FL_LAST_UPDATE_DATE = 'last_update_date';

    /**
     * (PHP 5)<br/>
     * Get the guild id 
     * @param string $guild_name
     * @return int
     */
    private function get_guild_id_from_name($guild_name){
        $sql_cmd = 'SELECT '.guild_DA::FL_ID.' FROM '.$this->get_table_prefix().'_guild WHERE '.guild_DA::FL_NAME.' = '.$this->format_string_to_sql_param($guild_name);

//        echo '<br>guild_DA->get_guild_id_from_name : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);

        if (count($result) != 0) {
                return $result[0][0];
        }

        return null;
    }


    /**
     * (PHP 5)<br/>
     * Save guild in the database
     * @param Guild $guild <p>
     * the guild that will be created. $guild->get_id() == null if the guild don't exists in DB
     * @return int guild id. <p> On errors, it will return false.
     */
    public function save_guild($guild){


        try {
            $char_DA = new character_DA();

            $guild_id = null;

            //if guild does not exists in db
            if ($guild->get_id() == null) {
                //insert guild data
                $guild_id = $this->create_guild($guild);

                //insert members data
                $member = new Guild_Member();
                foreach($guild->get_member_list() as $member){
                    $char_DA->create_guild_member($member, $guild_id, true);
                }
            }
            else {
                $guild_id = $guild->get_id();

                //update information
                $this->update_guild($guild);

                //get DB member list
                $member_list_in_db = $this->get_guild_member($guild_id);

                //compare DB member list and new member list
                //detect new member
                //member leaving
                //new grade
                foreach($guild->get_member_list() as $member){
                    $char_id = $char_DA->get_character_id($member->get_name());

                    //if member does not exist
                    if (!isset($char_id)) {
                        $char_DA->create_guild_member($member, $guild_id, false);
                    }
                    //else check if member grade has changed
                    else {

                        if (count($member_list_in_db[0]) > 0) {
                            $this->check_character_update_grade($member, $char_id, $member_list_in_db, $guild_id);
                        }
                    }
                }

                //check if a member as leave the guild
                $this->check_character_leaving_guild($guild, $member_list_in_db);
            }

            return $guild_id;
        }
        catch (Exception $ex)
        {
            echo 'Exception : ',  $ex->getMessage(), "\n";
        }

        return (isset($ex));
    }

    /**
     * (PHP 5)<br/>
     * Check if a guild member has a new grade
     * @param Guild_Member $member
     * @param int $char_id member id
     * @param array $member_list_in_db
     */
    private function check_character_update_grade($member, $char_id, $member_list_in_db, $guild_id) {
        //TODO : replace foreach by while
        $member_in_db = new Guild_Member();
        $char_DA = new character_DA();

        foreach($member_list_in_db as $member_in_db){

            if (($member_in_db[character_DA::FL_NAME] == $member->get_name())
                    && ($member_in_db[character_DA::FL_GRADE] != $member->get_grade_id())){

                $char_DA->update_character_grade($member, $char_id, $guild_id);
            }
        }
    }

    /**
     * (PHP 5)<br/>
     * Check if a guild member has leave the guild
     * @param Guild $guild
     * @param array $member_list_in_db
     */
    private function check_character_leaving_guild($guild, $member_list_in_db){

        $member_in_db = new Guild_Member();

        foreach($member_list_in_db as $member_in_db){
            $found = false;

            //TODO : replace foreach by while
            foreach($guild->get_member_list() as $member){
                if (($member_in_db[character_DA::FL_NAME] == $member->get_name())){
                    $found = true;
                }
            }

            if (!$found) {
                $char_DA = new character_DA();
                $char_DA->delete_character_leaving_guild($member_in_db[character_DA::FL_NAME], $member_in_db[character_DA::FL_ID], $guild->get_id());
            }
        }
    }

    /**
     * (PHP 5)<br/>
     * Get a guild member
     * @param int $guild_id
     * @return array of Guild_Member
     */
    private function get_guild_member($guild_id) {
        $sql_cmd = 'SELECT c.'.character_DA::FL_NAME.', c.'.character_DA::FL_GRADE.', c.'.character_DA::FL_API_KEY.', c.'.character_DA::FL_ID.'
                    FROM '.$this->get_table_prefix().'_guild g
                    INNER JOIN '.$this->get_table_prefix().'_character c
                    ON c.'.character_DA::FL_FK_GUILD_ID.' = g.'.guild_DA::FL_ID.'
                    AND g.'.guild_DA::FL_ID.' = '.$guild_id;

//        echo '<br>guild_DA->get_guild_member : '.$sql_cmd;

        return $member_list = $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Get a guild member (all informations : skills, fames, etc)
     * @param int $guild_id
     * @return array array of Character
     */
    public function get_guild_member_full($guild_id) {
        $result = array();

        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_character WHERE '.character_DA::FL_FK_GUILD_ID.' = '.$guild_id
                        .' ORDER BY '.character_DA::FL_GRADE.', '.character_DA::FL_NAME;

//        echo '<br>guild_DA->get_guild_member_full : '.$sql_cmd;

        $member_list = $this->execute_sql_command($sql_cmd, false);

        if (isset($member_list)){
            foreach($member_list as $char){

                $character = new Character();

                $character->set_general_information($char[character_DA::FL_ID], $char[character_DA::FL_NAME], $char[character_DA::FL_SHARD], $char[character_DA::FL_RACE], $char[character_DA::FL_GENDER],
                        $char[character_DA::FL_TITLEID], $char[character_DA::FL_MONEY], $char[character_DA::FL_GRADE], $char[character_DA::FL_JOINED_DATE], $char[character_DA::FL_LAST_UPDATE_DATE],
                        NULL,NULL);

                $character->set_fight_skill($char[character_DA::FL_MACE_1H], $char[character_DA::FL_STAFF_1H], $char[character_DA::FL_SPEAR_1H], $char[character_DA::FL_AXE_1H],
                        $char[character_DA::FL_SWORD_1H], $char[character_DA::FL_DAGGER_1H], $char[character_DA::FL_MACE_2H], $char[character_DA::FL_PIKE_2H], $char[character_DA::FL_AXE_2H],
                        $char[character_DA::FL_SWORD_2H], $char[character_DA::FL_HAND_TO_HAND], $char[character_DA::FL_PISTOL], $char[character_DA::FL_AUTOLAUNCHER],
                        $char[character_DA::FL_LAUNCHER], $char[character_DA::FL_RIFLE]);

                $character->set_magic_skill($char[character_DA::FL_ELEMENTAL_MAGIC], $char[character_DA::FL_OFFENSIVE_AFFICTION], $char[character_DA::FL_HEALING_MAGIC],
                        $char[character_DA::FL_DEFENSIVE_AFFLICTION]);

                $character->set_harvest_skill($char[character_DA::FL_DESERT_HARVEST], $char[character_DA::FL_JUNGLE_HARVEST], $char[character_DA::FL_LAKE_HARVEST],
                        $char[character_DA::FL_PRIME_ROOTS_HARVEST], $char[character_DA::FL_FOREST_HARVEST]);

                $character->set_craft_armor_item_skill($char[character_DA::FL_CRAFT_HEAVY_ARMOR_BOOT], $char[character_DA::FL_CRAFT_HEAVY_ARMOR_GLOVE], $char[character_DA::FL_CRAFT_HEAVY_ARMOR_HELMET],
                        $char[character_DA::FL_CRAFT_HEAVY_ARMOR_PANT], $char[character_DA::FL_CRAFT_HEAVY_ARMOR_SLEEVE], $char[character_DA::FL_CRAFT_HEAVY_ARMOR_VEST], $char[character_DA::FL_CRAFT_MEDIUM_ARMOR_BOOT],
                        $char[character_DA::FL_CRAFT_MEDIUM_ARMOR_GLOVE], $char[character_DA::FL_CRAFT_MEDIUM_ARMOR_PANT], $char[character_DA::FL_CRAFT_MEDIUM_ARMOR_SLEEVE], $char[character_DA::FL_CRAFT_MEDIUM_ARMOR_VEST],
                        $char[character_DA::FL_CRAFT_LIGHT_ARMOR_BOOT], $char[character_DA::FL_CRAFT_LIGHT_ARMOR_GLOVE], $char[character_DA::FL_CRAFT_LIGHT_ARMOR_PANT], $char[character_DA::FL_CRAFT_LIGHT_ARMOR_SLEEVE],
                        $char[character_DA::FL_CRAFT_LIGHT_ARMOR_VEST], $char[character_DA::FL_CRAFT_SHIELD], $char[character_DA::FL_CRAFT_LARGE_SHIELD]);

                $character->set_craft_jewel_item_skill($char[character_DA::FL_CRAFT_JEWEL_ANKLET], $char[character_DA::FL_CRAFT_JEWEL_BRACELET], $char[character_DA::FL_CRAFT_JEWEL_DIADEM],
                        $char[character_DA::FL_CRAFT_JEWEL_EARRING], $char[character_DA::FL_CRAFT_JEWEL_PENDANT], $char[character_DA::FL_CRAFT_JEWEL_RING]);

                $character->set_craft_weapon_skill($char[character_DA::FL_CRAFT_AXE_1H], $char[character_DA::FL_CRAFT_DAGGER_1H], $char[character_DA::FL_CRAFT_MACE_1H], $char[character_DA::FL_CRAFT_SPEAR_1H],
                        $char[character_DA::FL_CRAFT_SWORD_1H], $char[character_DA::FL_CRAFT_STAFF_1H], $char[character_DA::FL_CRAFT_AXE_2H], $char[character_DA::FL_CRAFT_MACE_2H],
                        $char[character_DA::FL_CRAFT_PIKE_2H], $char[character_DA::FL_CRAFT_SWORD_2H], $char[character_DA::FL_CRAFT_AMPLI], $char[character_DA::FL_CRAFT_PISTOL],
                        $char[character_DA::FL_CRAFT_AUTOLAUNCHER], $char[character_DA::FL_CRAFT_LAUNCHER], $char[character_DA::FL_CRAFT_RIFLE]);

                $character->set_statistics($char[character_DA::FL_CONSTITUTION], $char[character_DA::FL_METABOLISM], $char[character_DA::FL_INTELLIGENCE], $char[character_DA::FL_WISDOM],
                        $char[character_DA::FL_STRENGTH], $char[character_DA::FL_WELLBALANCED], $char[character_DA::FL_DEXTERITY], $char[character_DA::FL_WILL], $char[character_DA::FL_HITPOINTS],
                        $char[character_DA::FL_STAMINA], $char[character_DA::FL_SAP], $char[character_DA::FL_FOCUS]);

                $character->set_fame($char[character_DA::FL_CIVILIZATION], $char[character_DA::FL_CULT], $char[character_DA::FL_FYROS_FAME], $char[character_DA::FL_MATIS_FAME],
                        $char[character_DA::FL_TRYKER_FAME], $char[character_DA::FL_ZORAI_FAME], $char[character_DA::FL_KAMI_FAME], $char[character_DA::FL_KARAVAN_FAME],
                        $char[character_DA::FL_TRIBE_DARKENING_SAP_FAME], $char[character_DA::FL_TRIBE_LAWLESS_FAME], $char[character_DA::FL_TRIBE_LEVIERS_FAME],
                        $char[character_DA::FL_TRIBE_MASTER_OF_THE_GOO_FAME], $char[character_DA::FL_TRIBE_RESTORERS_FAME], $char[character_DA::FL_TRIBE_SCORCHERS_FAME], $char[character_DA::FL_TRIBE_WATER_BREAKERS_FAME]);

                $character->set_faction_point($char[character_DA::FL_FYROS_FACTION_POINT], $char[character_DA::FL_MATIS_FACTION_POINT], $char[character_DA::FL_TRYKER_FACTION_POINT],
                        $char[character_DA::FL_ZORAI_FACTION_POINT], $char[character_DA::FL_KAMI_FACTION_POINT], $char[character_DA::FL_KARAVAN_FACTION_POINT]);

                $character->set_connection_information($char[character_DA::FL_PLAYED_TIME], $char[character_DA::FL_LATEST_LOGIN], $char[character_DA::FL_LATEST_LOGOUT], null);

                $character->set_body_information($char[character_DA::FL_HAIR_TYPE], $char[character_DA::FL_HAIR_COLOR], $char[character_DA::FL_TATTOO], $char[character_DA::FL_EYES_COLOR],
                        $char[character_DA::FL_GABARIT_HEIGHT], $char[character_DA::FL_GABARIT_TORSO_WIDTH], $char[character_DA::FL_GABARIT_ARMS_WIDTH], $char[character_DA::FL_GABARIT_LEGS_WIDTH],
                        $char[character_DA::FL_GABARIT_BREAST_SIZE], $char[character_DA::FL_MORPH1], $char[character_DA::FL_MORPH2], $char[character_DA::FL_MORPH3], $char[character_DA::FL_MORPH4],
                        $char[character_DA::FL_MORPH5], $char[character_DA::FL_MORPH6], $char[character_DA::FL_MORPH7], $char[character_DA::FL_MORPH8]);

                $result[] = $character;
            }
        }



        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Create guild in the database
     * @param Guild $guild
     */
    private function create_guild($guild){
        //TODO : add fields
        $sql_cmd = 'INSERT INTO '.$this->get_table_prefix().'_guild
                    ('.guild_DA::FL_NAME.', '.guild_DA::FL_API_KEY.', '.guild_DA::FL_RACE.', '.guild_DA::FL_ICON.',
                    '.guild_DA::FL_SHARD.', '.guild_DA::FL_DESCRIPTION.', '.guild_DA::FL_MONEY.', '.guild_DA::FL_CIVILIZATION.',
                    '.guild_DA::FL_CULT.', '.guild_DA::FL_FYROS_FAME.', '.guild_DA::FL_KAMI_FAME.', '.guild_DA::FL_KARAVAN_FAME.',
                    '.guild_DA::FL_MATIS_FAME.', '.guild_DA::FL_TRYKER_FAME.', '.guild_DA::FL_ZORAI_FAME.', '.guild_DA::FL_LAST_UPDATE_DATE.')
                    VALUES ('.$this->format_string_to_sql_param($guild->get_name()).', '.$this->format_string_to_sql_param($guild->get_api_key()).',
                        '.$this->format_string_to_sql_param($guild->get_race()).', '. $guild->get_icon().',
                        '.$this->format_string_to_sql_param($guild->get_shard()).', '.$this->format_string_to_sql_param($guild->get_description()).',
                        '.$guild->get_money().','.$this->format_string_to_sql_param($guild->get_civilization()).',
                        '.$this->format_string_to_sql_param($guild->get_cult()).','.$guild->get_fyros_fame().','.$guild->get_kami_fame().','.$guild->get_karavan_fame().',
                        '.$guild->get_matis_fame().','.$guild->get_tryker_fame().','.$guild->get_zorai_fame().','.$this->format_date_to_sql_param(date('Y-m-d H:i:s',time())).')';

//        echo '<br>guild_DA->create_guild : '.$sql_cmd;

        $guild_id = $this->execute_sql_command($sql_cmd, true);

        return $guild_id;
    }

    /**
     * (PHP 5)<br/>
     * Update guild information in the database
     * @param Guild $guild
     */
    private function update_guild($guild){
        //TODO : add fields
        $sql_cmd = 'UPDATE '.$this->get_table_prefix().'_guild
                    SET '.guild_DA::FL_NAME.' = '.$this->format_string_to_sql_param($guild->get_name()).', '.guild_DA::FL_RACE.' = '.$this->format_string_to_sql_param($guild->get_race()).',
                        '.guild_DA::FL_ICON.' = '.$guild->get_icon().', '.guild_DA::FL_SHARD.' = '.$this->format_string_to_sql_param($guild->get_shard()).', '.guild_DA::FL_DESCRIPTION.' = '.$this->format_string_to_sql_param($guild->get_description()).',
                        '.guild_DA::FL_MONEY.' = '.$guild->get_money().', '.guild_DA::FL_CIVILIZATION.' = '.$this->format_string_to_sql_param($guild->get_civilization()).', '.guild_DA::FL_CULT.' = '.$this->format_string_to_sql_param($guild->get_cult()).',
                        '.guild_DA::FL_FYROS_FAME.' = '.$guild->get_fyros_fame().', '.guild_DA::FL_KAMI_FAME.' = '.$guild->get_kami_fame().', '.guild_DA::FL_KARAVAN_FAME.' = '.$guild->get_karavan_fame().', '.guild_DA::FL_MATIS_FAME.' = '.$guild->get_matis_fame().',
                        '.guild_DA::FL_TRYKER_FAME.' = '.$guild->get_tryker_fame().', '.guild_DA::FL_ZORAI_FAME.' = '.$guild->get_zorai_fame().', '.guild_DA::FL_LAST_UPDATE_DATE.' = '.$this->format_date_to_sql_param(date('Y-m-d H:i:s',time())).'
                    WHERE '.guild_DA::FL_ID.' = '.$guild->get_id();


//        echo '<br>guild_DA->update_guild : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Update guild api key
     * @param int $guild_id
     * @param string $api_key
     */
    public function update_guild_api_key($guild_id, $api_key){
        $sql_cmd = 'UPDATE '.$this->get_table_prefix().'_guild
                    SET '.guild_DA::FL_API_KEY.' = '.$this->format_string_to_sql_param($api_key).'
                    WHERE '.guild_DA::FL_ID.' = '.$guild_id;

//        echo '<br>guild_DA->update_guild_api_key : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Get all guild api key in the db
     * @return array of string
     */
    public function get_all_api_key(){
        $sql_cmd = 'SELECT '.guild_DA::FL_API_KEY.', '.guild_DA::FL_ID.', '.guild_DA::FL_NAME.' FROM '.$this->get_table_prefix().'_guild';

//        echo '<br>guild_DA->get_all_api_key : '.$sql_cmd;

        return $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Get guild api key in the db
     * @return array of string
     */
    public function get_guild_api_key($guild_id){
        $sql_cmd = 'SELECT '.guild_DA::FL_API_KEY.', '.guild_DA::FL_ID.', '.guild_DA::FL_NAME
                    .' FROM '.$this->get_table_prefix().'_guild WHERE '.guild_DA::FL_ID.' = '.$guild_id;

//        echo '<br>guild_DA->get_guild_api_key : '.$sql_cmd;

        return $this->execute_sql_command($sql_cmd, false);
    }


    /**
     * (PHP 5)<br/>
     * Get guild information
     * @param int $id
     * @return  Guild
     */
    public function get_guild($id){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_guild WHERE '.guild_DA::FL_ID.' = '.$id;

//        echo '<br>guild_DA->get_guild : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);

        if (isset($result[0])){
            $g = $result[0];
            $guild = new Guild();

            $guild->set_general_information($g[guild_DA::FL_ID], $g[guild_DA::FL_API_KEY], $g[guild_DA::FL_NAME], $g[guild_DA::FL_RACE],
                    $g[guild_DA::FL_ICON], $g[guild_DA::FL_SHARD], $g[guild_DA::FL_DESCRIPTION], $g[guild_DA::FL_CREATION_DATE], $g[guild_DA::FL_MONEY],
                    $g[guild_DA::FL_LAST_UPDATE_DATE]);



            $guild->set_fame($g[guild_DA::FL_CIVILIZATION], $g[guild_DA::FL_CULT], $g[guild_DA::FL_FYROS_FAME], $g[guild_DA::FL_MATIS_FAME],
                    $g[guild_DA::FL_TRYKER_FAME], $g[guild_DA::FL_ZORAI_FAME], $g[guild_DA::FL_KAMI_FAME], $g[guild_DA::FL_KARAVAN_FAME]);


            $char_DA = new character_DA();

            $guild->set_member_list($char_DA->get_guild_member_list($id));
        }



        return (isset($guild) ? $guild : null);
    }

    /**
     * (PHP 5)<br/>
     * Get guild information light (name, icon, id)
     * @param int $id
     * @return  Guild
     */
    public function get_guild_light($id){
        $sql_cmd = 'SELECT '.guild_DA::FL_ID.', '.guild_DA::FL_NAME.', '.guild_DA::FL_ICON.' FROM '.$this->get_table_prefix().'_guild WHERE '.guild_DA::FL_ID.' = '.$id;

//        echo '<br>guild_DA->get_guild_light : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);

        if (isset($result[0])){
            $g = $result[0];
            $guild = new Guild();

            $guild->set_general_information($g[guild_DA::FL_ID], null, $g[guild_DA::FL_NAME], null, $g[guild_DA::FL_ICON], null,
                    null, null, null, null);
        }

        return (isset($guild) ? $guild : null);
    }


    /**
     * (PHP 5)<br/>
     * Get primary guild light (name, icon, id)
     * @return int guild id
     */
    public function get_primary_guild_light(){
        //TODO : add new flag in guild table.
        $sql_cmd = 'SELECT '.guild_DA::FL_ID.', '.guild_DA::FL_NAME.', '.guild_DA::FL_ICON.' FROM '.$this->get_table_prefix().'_guild ORDER BY '.guild_DA::FL_ID.' LIMIT 1 ;';

//        echo '<br>guild_DA->get_guild_light : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);

        if (isset($result[0])){
            $g = $result[0];
            $guild = new Guild();

            $guild->set_general_information($g[guild_DA::FL_ID], null, $g[guild_DA::FL_NAME], null, $g[guild_DA::FL_ICON], null,
                    null, null, null, null);
        }

        return (isset($guild) ? $guild : null);


    }


    /**
     * (PHP 5)<br/>
     * Get guild list
     * @return array of Guild
     */
    public function get_guild_list(){
        $sql_cmd = 'SELECT '.guild_DA::FL_ID.', '.guild_DA::FL_NAME.',
                    '.guild_DA::FL_ICON.', '.guild_DA::FL_CREATION_DATE.' FROM '.$this->get_table_prefix().'_guild';

//        echo '<br>guild_DA->get_guild_list : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);

        if (isset($result[0])){
            foreach ($result as $g){
                $guild = new Guild();
                $guild->set_general_information($g[guild_DA::FL_ID], null, $g[guild_DA::FL_NAME], null, $g[guild_DA::FL_ICON],
                        null, null, $g[guild_DA::FL_CREATION_DATE], null, null);

                $guild_array[] = $guild;
            }
        }

        return (isset($guild_array) ? $guild_array : null);
    }

}

?>
